#!/bin/bash

RETRIES="{{ handler_health_rgw_check_retries }}"
DELAY="{{ handler_health_rgw_check_delay }}"
RGW_NAME="{{ ansible_hostname }}"
RGW_PORT="{{ radosgw_civetweb_port }}"
SOCKET=/var/run/ceph/{{ cluster }}-client.rgw.${RGW_NAME}.asok
{% if containerized_deployment %}
DOCKER_EXEC="docker exec ceph-rgw-{{ ansible_hostname }}"
{% endif %}
{% if hostvars[inventory_hostname]['radosgw_address_block'] is defined and hostvars[inventory_hostname]['radosgw_address_block'] != 'subnet' %}
    {% if ip_version == 'ipv4' %}
RGW_IP={{ hostvars[inventory_hostname]['ansible_all_' + ip_version + '_addresses'] | ipaddr(radosgw_address_block) | first }} \
    {% elif ip_version == 'ipv6' %}
RGW_IP=[{{ hostvars[inventory_hostname]['ansible_all_' + ip_version + '_addresses'] | ipaddr(radosgw_address_block) | first }}] \
    {% endif %}
{% elif radosgw_address_block is defined and radosgw_address_block != 'subnet' -%}
    {% if ip_version == 'ipv4' %}
RGW_IP={{ hostvars[inventory_hostname]['ansible_all_' + ip_version + '_addresses'] | ipaddr(radosgw_address_block) | first }} \
    {% elif ip_version == 'ipv6' %}
RGW_IP=[{{ hostvars[inventory_hostname]['ansible_all_' + ip_version + '_addresses'] | ipaddr(radosgw_address_block) | first }}] \
    {% endif %}
{% elif hostvars[inventory_hostname]['radosgw_address'] is defined and hostvars[inventory_hostname]['radosgw_address'] != 'address' -%}
    {% if ip_version == 'ipv4' %}
RGW_IP={{ hostvars[inventory_hostname]['radosgw_address'] }} \
    {% elif ip_version == 'ipv6' %}
RGW_IP=[{{ hostvars[inventory_hostname]['radosgw_address'] }}] \
    {% endif %}
{% elif radosgw_address is defined and radosgw_address != 'address' -%}
    {% if ip_version == 'ipv4' %}
RGW_IP={{ radosgw_address }} \
    {% elif ip_version == 'ipv6' %}
RGW_IP=[{{ radosgw_address }}] \
    {% endif %}
{% elif hostvars[inventory_hostname]['radosgw_interface'] is defined -%}
    {% set interface = 'ansible_' + hostvars[inventory_hostname]['radosgw_interface'] %}
    {% if ip_version == 'ipv4' %}
RGW_IP={{ hostvars[inventory_hostname][interface][ip_version]['address'] }} \
    {% elif ip_version == 'ipv6' %}
RGW_IP=[{{ hostvars[inventory_hostname][interface][ip_version][0]['address'] }}] \
    {% endif %}
{% else %}
    {% set interface = 'ansible_' + radosgw_interface %}
    {% if ip_version == 'ipv4' %}
RGW_IP={{ hostvars[inventory_hostname][interface][ip_version]['address'] }} \
    {% elif ip_version == 'ipv6' %}
RGW_IP=[{{ hostvars[inventory_hostname][interface][ip_version][0]['address'] }}] \
    {% endif %}
{% endif %}

check_for_curl_or_wget() {
  if $DOCKER_EXEC command -v wget &>/dev/null; then
    rgw_test_command="wget --quiet"
  elif $DOCKER_EXEC command -v curl &>/dev/null; then
    rgw_test_command="curl --fail --silent --output /dev/null"
  else
    echo "It seems that neither curl or wget are available on your system."
    echo "Cannot test rgw connection."
    exit 0
  fi
}

check_rest() {
  check_for_curl_or_wget
  while [ $RETRIES -ne 0 ]; do
    test "$rgw_test_command http://$RGW_IP:$RGW_PORT" && exit 0
    sleep $DELAY
    let RETRIES=RETRIES-1
  done
  # If we reach this point, it means there is a problem with the connection to rgw
  echo "Error connecting locally to Rados Gateway service: http://$rgw_listen"
  exit 1
}

# First, restart the daemon
systemctl restart ceph-radosgw@rgw.${RGW_NAME}

COUNT=10
# Wait and ensure the socket exists after restarting the daemon
while [ $COUNT -ne 0 ]; do
  $DOCKER_EXEC test -S $SOCKET && check_rest
  sleep $DELAY
  let COUNT=COUNT-1
done
echo "Socket file ${SOCKET} could not be found, which means Rados Gateway is not running."
exit 1
